/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxinline.servlet;

import br.com.foxinline.modelo.Anexo;
import br.com.foxinline.servico.AnexoServico;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author roniere
 */
@WebServlet(name = "AnnexShowServlet", urlPatterns = "/show/*")
public class AnnexShowServlet extends HttpServlet {

    @EJB
    AnexoServico anexoServico;
    private static final long serialVersionUID = 201011231103L;

    public AnnexShowServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OutputStream out;
        
        try {
            String caminhoImagem = request.getParameter("annex");
            
            Anexo annex = new Anexo();
            
            String caminhoImagemExibicao = caminhoImagem;
            
            if (caminhoImagem != null) {
                out = response.getOutputStream();
                response.setContentType(annex.getTipo());
                response.getOutputStream().write(Files.readAllBytes(Paths.get(caminhoImagemExibicao)));
            } else {
                out = response.getOutputStream();
                response.setContentType("image/jpg");
            }

            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
